package com.alibobo.bigsixnineapp.activities;

import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;

import com.alibobo.bigsixnineapp.R;
import com.alibobo.tech5.ganji.CalendarHelper;
import com.alibobo.utils.calendar.DateUtils;
import com.alibobo.utils.calendar.YMDHMS;

public class SelectTimeType1Activity extends Activity {

	Button _dateBtn;
	Button _jiBtn;

	Intent _intent;

	Bundle _srcBundle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		_intent = getIntent();
		_srcBundle = _intent.getExtras();

		setContentView(R.layout.activity_select_time_type1);
		_dateBtn = (Button) findViewById(R.id.YMDbutton1);
		_jiBtn = (Button) findViewById(R.id.HourJibutton1);
		// CalendarHelper

		String t1 = DateUtils.getYYYYMD(new Date());
		String t2 = CalendarHelper.getCurrentJi();
		_dateBtn.setText(t1);
		_jiBtn.setText(t2);
		initDateSelect();
		initJiSelect();
		initCancel();
		initOK();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.select_time_type1, menu);
		return true;
	}

	protected void initDateSelect() {
		_dateBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				showDatePick();
			}
		});

	}

	protected void showDatePick() {
		YMDHMS ymd = new YMDHMS(new Date());
		new DatePickerDialog(this, new OnDateSetListener() {

			@Override
			public void onDateSet(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				String ss = String.format("%d/%d/%d", year, monthOfYear + 1,
						dayOfMonth);
				_dateBtn.setText(ss);
			}
		}, ymd.year, ymd.month, ymd.day).show();
	}

	/*********************************************************************
	 * 
	 * 
	 ********************************************************************/
	protected void initJiSelect() {

		_jiBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showSelectJi();

			}
		});
	}

	protected void showSelectJi() {
		new AlertDialog.Builder(SelectTimeType1Activity.this).setTitle("選時")
				.setItems(R.array.ji12, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						String[] _ji = getResources().getStringArray(
								R.array.ji12);
						_jiBtn.setText(_ji[which]);
					}
				}).show();
	}

	protected void initCancel() {
		((Button) findViewById(R.id.buttonCancel))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						SelectTimeType1Activity.this.setResult(-1);
						SelectTimeType1Activity.this.finish();
					}
				});
	}

	protected void initOK() {
		((Button) findViewById(R.id.ButtonOK))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						String s1=_dateBtn.getText().toString();
						String s2=_jiBtn.getText().toString();
						
						_intent.putExtra("date", s1);
						_intent.putExtra("ji", s2);
						SelectTimeType1Activity.this.setResult(1, _intent);
						SelectTimeType1Activity.this.finish();
					}
				});
	}
}
